<template>
  <div class="app-container">
    <div class="filter-container">

      <span class="filter-title filter-item">红包时间</span>
      <k-date-picker
        v-model="timeValue"
        class="filter-item"
        @hqchang="handleFilter"

      />
      <k-input
        v-model.trim="listQuery.key"
        class="filter-item"
        is-search
        placeholder="请输入关键词"
        :search-fun="handleFilter"
        @keyup.enter.native="handleFilter"
      />
    </div>
    <!-- 创建 roll 房 -->
    <k-button class="top-btn" value="签到红包配置" @click.native="creatBloodDialog" />
    <!-- <k-button class="top-btn" value="玩法介绍" @click.native="ruleGuideDialog" /> -->
    <el-switch
      v-model="signRedpocketSwitch"
      class="el_switch_top"
      active-color="#3b425d"
      inactive-color="#E9EBF2"
      active-text="开启"
      inactive-text="关闭"
      :active-value="1"
      :inactive-value="0"
      :disabled="!authorityHandle(53)"
      @change="setSignStatus()"
    />
    <!--表格相关 -->
    <el-table
      :data="signTableData"
      fit
      border
      :show-summary="!authorityHandle(55)"
      :summary-method="getSummaries"
      highlight-current-row
      style="width: 100%;border-radius:8px;"
      :header-cell-style="{background:'#F6F5FA',color:'#4E4F62'}"
    >
      <el-table-column width="80px" type="index" label="序号" align="center" />
      <el-table-column prop="userName" min-width="100px" label="昵称" align="center" show-overflow-tooltip />
      <el-table-column prop="userMobile" min-width="100px" label="用户名" align="center" show-overflow-tooltip />
      <el-table-column prop="amount" min-width="100px" label="领取金额" align="center" show-overflow-tooltip />
      <el-table-column prop="receiveTime" min-width="100px" label="领取时间" align="center" show-overflow-tooltip />
      <el-table-column prop="count" min-width="90px" label="签到/领取次数" align="center" />
    </el-table>
    <pagination
      v-show="totalRows > 0"
      :total="totalRows"
      :page.sync="listQuery.pager.pageNo"
      :limit.sync="listQuery.pager.pageSize"
      @pagination="loadData"
    />
    <!-- 玩法介绍 弹窗 -->
    <k-dialog
      :show-hide.sync="ruleGuide.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      title="玩法介绍"
      class-name="ruleGuide-dialog"
      :show-foot="false"
      :two-btn="authorityHandle(53)"
      @click="guideSave"
    >
      <div class="dialog-ruleGuide" style="padding:30px">
        <el-form :model="ruleGuideContent" label-width="auto">
          <el-form-item label="内容介绍：" prop="content">
            <quill-editor
              ref="myQuillEditor"
              v-model.trim="ruleGuideContent.content"
              :options="editorOption"
              class="editer"
            />
          </el-form-item>
        </el-form>
      </div>
    </k-dialog>

    <!-- 创建红包 弹窗 -->
    <k-dialog
      :show-hide.sync="creatRollRoom.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      title="签到红包配置"
      class-name="creatRollRoom_dialog"
      :show-foot="false"
      :two-btn="authorityHandle(53)"
      @click="saveBloodRed"
    >
      <div class="set_box">
        <div v-for="(item,index) in signRedSettingItemList" :key="item.id" class="Recharge_Data">
          <div class="Recharge_Data_box">
            <span>红包档位:</span>
            <el-input :readonly="!authorityHandle(53)" v-model.trim="item.minAmount" />
            <i style="margin: 0 2px;"> — </i>
            <el-input :readonly="!authorityHandle(53)" v-model.trim="item.maxAmount" style="margin-right:20px" />
            <span>档位概率:</span>
            <el-input :readonly="!authorityHandle(53)" v-model.trim="item.probability" /> %
          </div>
          <div class="operation">
            <i v-if="authorityHandle(53)" v-show="index != 0" class="el-icon-remove" @click="deleteItem(item.id,index)" />
            <i v-if="authorityHandle(53)" v-show="index == signRedSettingItemList.length - 1" class="el-icon-circle-plus" @click="newlyAdded()" />
          </div>
        </div>
      </div>
    </k-dialog>

  </div>
</template>
<script>
import { getSignData, setSignData, getSignStatus, setSignStatus, listSignData, getDescribe, setDescribe } from '@/api/red/data'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      amount: 0,
      signRedpocketSwitch: 1,
      totalRows: 0, // 分页组件--数据总条数
      listQuery: {
        pager: {
          pageNo: 1,
          pageSize: 10,
          totalRows: '',
          start: ''
        },
        sort: '', // 排序字段 为对应列名,
        direction: '', // 排序 正向(asc)逆向(desc),
        startTime: undefined, // 饰品外观',
        endTime: undefined, // 饰品类别',
        key: '' // 箱子名称或关键词 箱子配置界面所用'
      },
      signTableData: [],
      ruleGuideContent: {
        content: ''
      },
      signRedSettingItemList: [{
        id: undefined,
        minAmount: undefined,
        maxAmount: undefined,
        probability: undefined
      }],
      editorOption: { //  富文本编辑器配置
        placeholder: '请输入内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'], // 引用，代码块
            [{ 'header': 1 }, { 'header': 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
            [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标
            [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
            [{ 'direction': 'rtl' }], // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 几级标题
            [{ 'color': [] }, { 'background': [] }], // 字体颜色，字体背景颜色
            [{ 'font': [] }], // 字体
            [{ 'align': [] }], // 对齐方式
            ['clean'], // 清除字体样式
            ['image'] // 上传图片、上传视频
          ]
        }
      },
      timeValue: '',
      conditions: {
        keyword: null
      },
      creatRollRoom: {
        showHide: false
      },
      userListDialog: {
        showHide: false
      },
      ruleGuide: {
        showHide: false
      }
    }
  },
  watch: {
    // 时间赋值
    timeValue(val) {
      this.listQuery.startTime = this.timeValue ? this.timeValue[0] : null
      this.listQuery.endTime = this.timeValue ? this.timeValue[1] : null
    }
  },
  created() {
    this.loadData()
    this.getSignData()
    this.getDescribe()
    this.getSignStatus()
  },
  methods: {
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) { // 第一列
          sums[index] = '总计'
        } else if (column.property === 'amount') {
          sums[index] = this.amount
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    loadData() {
      listSignData(this.listQuery).then(res => {
        this.totalRows = res.data.totalRows
        if (res.data.totalData) {
          this.amount = res.data.totalData.amount
        }
        this.signTableData = res.data.rows
      })
    },
    getSignData() {
      getSignData().then(res => {
        this.signRedSettingItemList = res.data
      })
    },
    // 获取支付方式开关
    getSignStatus() {
      getSignStatus().then(res => {
        this.signRedpocketSwitch = res.data
        this.$log(res.data, this.signRedpocketSwitch)
      })
    },
    setSignStatus() {
      setSignStatus({ 'status': this.signRedpocketSwitch }).then(res => {
        this.$log(res)
      })
    },
    // 设置新增
    newlyAdded() {
      this.signRedSettingItemList.push({
        id: undefined,
        minAmount: undefined,
        maxAmount: undefined,
        probability: undefined
      })
    },
    // 设置删除
    deleteItem(value, indexItem) {
      if (!value) {
        this.signRedSettingItemList.splice(indexItem, 1)
        return
      }
      var index = this.signRedSettingItemList.findIndex(function(item) {
        return item.id === value
      })
      if (index !== -1) {
        this.signRedSettingItemList.splice(index, 1)
      }
    },
    creatBloodDialog() {
      this.creatRollRoom.showHide = true
    },
    // saveBloodRed
    saveBloodRed() {
      const probSum = this.signRedSettingItemList.map(item => {
        return item.probability
      }).reduce(function(a, b) {
        return Number(a) + Number(b)
      })
      this.$log(probSum)
      if (100 - Number(probSum).toFixed(2) !== 0) {
        this.$message.error('档位概率和要等于100,还需要 ' + (100 - probSum).toFixed(2))
        return
      }
      setSignData(this.signRedSettingItemList).then(res => {
        this.$message.success('保存成功')
        this.creatRollRoom.showHide = false
      })
    },
    // 保存玩法管理
    guideSave() {
      const contents = this.ruleGuideContent.content
      setDescribe({ 'key': 'envelopeSign', 'data': contents }).then(res => {
        this.ruleGuide.showHide = false
      })
    },
    // 加载玩法管理
    getDescribe() {
      getDescribe({ key: 'envelopeSign' }).then(res => {
        this.ruleGuideContent.content = res.data.msg
      })
    },
    // 搜索查询事件
    handleFilter() {
      this.listQuery.pager.pageNo = 1
      this.loadData()
    },

    ruleGuideDialog() {
      this.ruleGuide.showHide = true
    }
  }

}
</script>
<style lang="scss" scoped>
$menuText:#14162E; // 侧边栏字体颜色
$hoverColor:#1D143C;// 鼠标停留主色调浅色
$mainColor:#3b425d;// 主色调
.filter-container {
  width: 100%;
  height: 48px;
  line-height: 48px;
  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }
  .kInput{
    position:absolute;
    right: 20px;
  }
  .filter-title{
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    font-weight: 500;
    margin-right: 22px;
    color: #14162E;
  }
  .user-item {
    margin-left: 10px;
    margin-right: 20px;
  }
  .room-type,.user-status,.account-status{
    margin-right: 20px;
  }
}
.red_money{
  width: 200px;
}
.grid-div{
  display: grid;
  grid-template-columns:  repeat(7, 120px);
  .item{
    height: 50px;
    font-size: 14px;
    line-height:50px;
    text-align: center;
    border: 1px solid #E3E2E7;
    width: 120px;
    overflow: hidden;
    display: inline-block;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 10px;
  }
}
.top-btn{
  margin: 30px 0px 30px 0px;
  font-size: 14px;
  color: #ffffff;
   &:nth-of-type(2){
    margin-left: 10px;
  }
}
.kSelect .el-select .el-input .el-input__inner{
  width: 124px;
}
::v-deep .el-select .el-input.is-focus .el-input__inner {
    border-color: $mainColor;
}
::v-deep .el-input__inner:focus {
    border-color: $mainColor;
}
.el-form-item__label-wrap .el-form-item__label{
  float: none;
}

.dialog_creatRollRoom{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 40px 80px;
  ::v-deep .el-select .el-select__tags>span {
			display: none;
	}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
   .el-tag {
    color: $mainColor;
    background-color: #ffffff;
    border-color: $mainColor;
    ::v-deep .el-tag__close {
      color: $mainColor;
    }
    ::v-deep .el-icon-close:hover {
      color:#ffffff;
      background: $mainColor;
    }
  }
}

.form-modify{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 10px 80px;
}

.editer {
  width: 500px;
  border-radius: 8px;
  float: left;
  box-sizing: border-box;
  color: #606266;
  ::v-deep .ql-container {
    height: 270px;
    border-radius: 0px 0px 8px 8px;
  }
  ::v-deep .ql-toolbar.ql-snow {
    border-radius: 8px 8px 0px 0px;
  }
}
.chargeform{
  display: flex;
  flex-direction: row;
}

::v-deep .el-switch__label.is-active {
    color: $mainColor;
}

.addBox_dialog{
  display: flex;
  flex-direction: column;
  justify-content: center;
  ::v-deep .el-dialog__body{
    width: 678px;
    margin: 40px 80px;
 }
::v-deep .el-select .el-select__tags>span {
			display: none;
	}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
   .el-tag {
    color: $mainColor;
    background-color: #ffffff;
    border-color: $mainColor;
    ::v-deep .el-tag__close {
      color: $mainColor;
    }
    ::v-deep .el-icon-close:hover {
      color:#ffffff;
      background: $mainColor;
    }
  }
}
.el_switch_top{
  margin: 30px 0px 30px 0px;
  position: absolute;
  right: 20px;
}

.set_box {
    width: 700px;
    margin: 0 80px;
    box-sizing: border-box;
    .Recharge_Data {
      width: 100%;
      height: 40px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      .Recharge_Data_box {
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .el-input {
          width: 150px;
          height: 35px;
          input {
            width: 150px;
            height: 35px;
          }
        }
        span {
          font-size: 14px;
          width: 100px;
        }
      }
      .operation {
        width: 12%;
        display: flex;
        justify-content: space-between;
        .el-icon{
            &-circle-plus, &-remove{
              font-size: 22px;
              color:#3b425d;
              margin-left: 10px;
          }
        }
      }
     }
 }
</style>
